<script setup>
import PageContainer from '@/components/pageContainer.vue'
import { ref } from 'vue'
import { useUserStore } from '@/store'
import { updateUserInfo } from '@/api/user'
import { ElMessage } from 'element-plus'
const {
  user: { id, username, nickname, email },
  getUser
} = useUserStore()

const form = ref({
  id,
  username,
  nickname,
  email
})

const rules = ref({
  nickname: [
    { required: true, message: '请输入用户昵称', trigger: 'blur' },
    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入用户邮箱', trigger: 'blur' },
    {
      type: 'email',
      message: '请输入正确的邮箱格式',
      trigger: ['blur', 'change']
    }
  ]
})
const formRef = ref()
const submitForm = async () => {
  await formRef.value.validate
  updateUserInfo(form.value)
  ElMessage.success('修改成功')
  //   通知user模块更新用户信息
  getUser()
}
</script>
<template>
  <page-container title="基本资料">
    <el-col :span="12">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="登录名称" prop="username">
          <el-input v-model="form.username" disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickname" required>
          <el-input v-model="form.nickname" show-require-mark></el-input>
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email" required>
          <el-input v-model="form.email" show-require-mark></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('form')"
            >提交修改</el-button
          >
        </el-form-item>
      </el-form>
    </el-col>
  </page-container>
</template>
